import pygame
from pygame.locals import *
from pygame import *
import sys, os
import label
import image
from config import config

class labellist(pygame.sprite.Sprite):
    def __init__(self, elelist, align, xpos, ypos, xsize, ysize, application):
        self.alllabels = []
        self.xpos = xpos
        self.ypos = ypos
        self.xsize = xsize
        self.ysize = ysize
        self.oysize = ysize # This for the length of the shown labels
        if self.ysize > len(elelist):
            self.ysize = len(elelist)
        self.oypos = ypos # ORIGINAL ysize, BECAUSE self.ysize WILL CHANGE
        self.upele = 0 # THIS IS THE ELEMENT AT THE TOP
        self.value = 0 # ACTUAL VALUE
        # GET THEME
        joyconf = config("joywm.xml")
        themefile = joyconf.conf.get('theme')
        themeconf = config(themefile)
        themename = themeconf.conf.get('name')
        bold = 0
        if themeconf.conf.get('bold') == 'True':
            bold = 1
        italic = 0
        if themeconf.conf.get('italic') == 'True':
            italic = 1
        # GET NAME FOR BAR
        name = 'themes/' + themename + '/widgets/bar.png'
        left_name = 'themes/' + themename + '/widgets/left_bar.png'
        right_name = 'themes/' + themename + '/widgets/right_bar.png'
        # LOAD BAR
        self.bar = image.image(name, 0, -20, 0, 0, application)
        self.bar.image = pygame.transform.scale(self.bar.image, ((self.xsize - 2) * application.cubex, application.cubey))
        self.leftbar = image.image(left_name,0, -20, 0, 0, application)
        self.leftbar.image = pygame.transform.scale(self.leftbar.image, (application.cubex, application.cubey))
        self.rightbar = image.image(right_name,0, -20, 0, 0, application)
        self.rightbar.image = pygame.transform.scale(self.rightbar.image, (application.cubex, application.cubex))
        # RENDER ALL ITEMS IN elelist
        for i in range (0, len(elelist)):
            self.alllabels.append(label.label(elelist[i], self.xsize, self.xpos+5, -1, application))
        # Show bar
        self.bar.hidebeamto(self.xpos + 1, self.ypos, application)
        self.leftbar.hidebeamto(self.xpos, self.ypos, application)
        self.rightbar.hidebeamto(self.xpos + self.xsize - 1, self.ypos, application)
        # SHOW ALL POSSIBLE LABELS
        cy = self.oypos * application.cubey
        for x in range (self.upele, self.ysize):
            self.alllabels[x].rect.y = cy
            cy += application.cubey
        # SHOW
        application.spritegrouprender.draw(pygame.display.get_surface())
        pygame.display.flip()

    def change(self, newlist, application):
        # DELETE ALL ITEMS
        for x in range (self.upele, self.upele + self.ysize):
            self.alllabels[x].rect.y = -self.alllabels[x].rect.height
        self.alllabels = []
        # RESET ALL TO POSITION 0
        self.bar.hidebeamto(self.xpos + 1, self.oypos, application)
        self.leftbar.hidebeamto(self.xpos, self.oypos, application)
        self.rightbar.hidebeamto(self.xpos + self.xsize - 1, self.oypos, application)
        self.upele = 0
        self.value = 0
        self.ypos = self.oypos
        if self.ysize <> self.oysize:
            self.ysize = self.oysize
        if self.ysize > len(newlist):
            self.ysize = len(newlist)
        # LOAD ALL NEW LABELS
        for i in range (0, len(newlist)):
            self.alllabels.append(label.label(newlist[i], self.xsize, self.xpos, -1, application))
        # SHOW ALL POSSIBLE LABELS
        #cy = self.oypos * 25
        cy = self.oypos * application.cubey
        for x in range (self.upele, self.ysize):
            self.alllabels[x].rect.y = cy
            #cy += 25
            cy += application.cubey
        # SHOW
        application.spritegrouprender.draw(pygame.display.get_surface())
        pygame.display.flip()

    def down(self, application):
        # Check if the end of shown elements is reached
        if self.value == self.upele + self.ysize - 1 and self.value <> len(self.alllabels)-1:
            # Change the shown labels
            self.value += 1
            self.upele += 1
            cy = self.oypos * application.cubey
            # Remove first label
            self.alllabels[self.upele-1].rect.y = -application.cubey
            # Move the rest one up
            cy = self.oypos * application.cubey
            for x in range(self.upele, self.upele + self.ysize):
                self.alllabels[x].rect.y = cy
                cy += application.cubey
        # Just move the bar (only if not the end of the list is reached!)
        else:
            if self.value <> len(self.alllabels)-1:
                self.value += 1
                self.ypos += 1
                self.bar.hidebeamto(self.xpos + 1, self.ypos, application)
                self.leftbar.hidebeamto(self.xpos, self.ypos, application)
                self.rightbar.hidebeamto(self.xpos + self.xsize - 1, self.ypos, application)
        # Show
        application.spritegrouprender.draw(pygame.display.get_surface())
        pygame.display.flip()

    def up(self, application):
        # Check if the top of the shown elements is reached
        # Move bar one element up
        if self.value > self.upele and self.value > 0:
            self.value -= 1
            self.ypos -= 1
            self.bar.hidebeamto(self.xpos + 1, self.ypos, application)
            self.leftbar.hidebeamto(self.xpos, self.ypos, application)
            self.rightbar.hidebeamto(self.xpos + self.xsize - 1, self.ypos, application)
        else:
            # Move list one element up
            if self.value > 0 and self.upele > 0:
                self.value -= 1
                self.upele -= 1
                cy = self.oypos * application.cubey
                # Remove last label
                self.alllabels[self.upele + self.ysize].rect.y = -application.cubey
                # Move th rest down
                for x in range(self.upele + 1, self.upele + self.ysize):
                    cy += application.cubey
                    self.alllabels[x].rect.y = cy
                # Show a new first element
                self.alllabels[self.upele].rect.y = self.oypos * application.cubey
        # Show
        application.spritegrouprender.draw(pygame.display.get_surface())
        pygame.display.flip()

    def pgdown(self, application):
        if self.upele < len(self.alllabels) - self.ysize:
            k = self.value - self.upele
            # Remove all shown labels
            for x in range(self.upele, self.upele + self.ysize):
                self.alllabels[x].rect.y = -application.cubey
            self.upele += self.ysize
            if self.upele > len(self.alllabels) - self.ysize:
                self.upele = len(self.alllabels) - self.ysize
            self.value = self.upele + k
            cy = self.oypos * application.cubey
            for x in range(self.upele, self.upele + self.ysize):
                self.alllabels[x].rect.y = cy
            cy += application.cubey
        else:
            # WE CANT GO PAGE DOWN, SO WE JUMP TO THE LAST ELEMENT
            j = len(self.alllabels) - 1 - self.value
            self.ypos += j
            self.value += j
            self.bar.hidebeamto(self.xpos + 1, self.ypos, application)
            self.leftbar.hidebeamto(self.xpos, self.ypos, application)
            self.rightbar.hidebeamto(self.xpos + self.xsize - 1, self.ypos, application)
        # Show
        application.spritegrouprender.draw(pygame.display.get_surface())
        pygame.display.flip()

    def pgup(self, application):
        if self.upele > 0:
            k = self.value - self.upele
            # Remove all shown labels
            for x in range(self.upele, self.upele + self.ysize):
                self.alllabels[x].rect.y = -application.cubey
            self.upele -= self.ysize
            if self.upele < 0:
                self.upele = 0
            self.value = self.upele + k
            cy = self.oypos * application.cubey
            for x in range(self.upele, self.upele + self.ysize):
                self.alllabels[x].rect.y = cy
            cy += application.cubey
        else:
            # WE CANT GO PAGE DOWN, SO WE JUMP TO THE FIRST ELEMENT
            self.ypos = self.oypos
            self.value = 0
            self.bar.hidebeamto(self.xpos + 1, self.ypos, application)
            self.leftbar.hidebeamto(self.xpos, self.ypos, application)
            self.rightbar.hidebeamto(self.xpos + self.xsize - 1, self.ypos, application)
        # Show
        application.spritegrouprender.draw(pygame.display.get_surface())
        pygame.display.flip()
